<?php
/**
 * 
 */
use Fox\MaterialManagement\Material\Hierarchy;
use Fox\MaterialManagement\Material;
use Doctrine\DBAL\Connection;
use Fox\Common\Customer;
$ajax = Xview::instance()->val("xvtrs");
$blItems = Xview::instance()->component("inquiry", "items"); 
$grid = $blItems->item("gItems");
if(!empty($ajax))
{
	$hiearchies = Hierarchy::loadTree();
	foreach($hiearchies as $hierarchy)
	{
		$parentid = $hierarchy['parentid'];
		$id = $hierarchy['id'];
		$hierarchyTree[$id] = $parentid;
		if(empty($parentid))
		{
			$materialHiearchy[$id]['label'] = $hierarchy['label'];
			$materialHiearchy[$id]['code'] =  $hierarchy['code'];
			$hierarchyLabel[$hierarchy['code']] = $hierarchy['label']; 
		}
		else 
		{
			$label = $code = null;
			$currentId = $id;
			while(isset($hierarchyTree[$currentId]))
			{
				$currentId = $hierarchyTree[$currentId];
				
				if(isset($materialHiearchy[$currentId]))
				{
					$label = $materialHiearchy[$currentId]['label']." / ".$label;
					$code = $materialHiearchy[$currentId]['code'].$code;
				}
			}
			
			$code .= $hierarchy['code'];
			$materialHiearchy[$id]['label'] = $hierarchy['label'];
			$materialHiearchy[$id]['code'] =  $hierarchy['code'];
			$hierarchyLabel[$code] = $label.$hierarchy['label'];
		}
	}

	$uniqueData = null;
	/* @var $grid Fox\Grid */
	$data = $grid->get();
	$root = new Fox\Node(Fox\Node::ROOT_NODE);
	if(is_array($data['gItems']))
	{
		foreach($data['gItems'] as $key => $line)
		{
			if(empty($line['materialid']))
				continue;
				
			if(isset($treeData[$line['parentid']]))
			{
				$treeData[$line['materialid']] = $treeData[$line['parentid']]->add(new Fox\Node($line));
			}
			else
			{
				$treeData[$line['materialid']] = $root->add(new Fox\Node($line));
			}
			
		}
	}

	$codes = Xview::instance()->val("codes");
	if(is_array($codes))
	{
		foreach($codes as $code)
		{
			list($materialid,$componentid) = preg_split("/:/",$code);
			$materialCode[] = $materialid;
			$linkMaterialBom[$componentid] = $materialid;
			$selectedComponent[$componentid] = true;
		}
		
		$finder = new Material();
		$materials = $finder->find(new Fox\Dao\SqlFragment("materialid IN (?)",array($materialCode),array(Connection::PARAM_INT_ARRAY)),
									array("hierarchy" => false));
		
		foreach($materials as $material)
		{
			/* @var $material Material */	
			$materialid = $material->getId();
			
			// Adding Bom Header
			if(!isset($treeData[$materialid]))
			{
				$data = $material->getData();
				$data['materialid'] = $materialid;
				$parentid = $materialid;
				// Check if the Material is not also in a BOM
				$bomParents = $material->bomParents();
				foreach($bomParents as $parent)
				{
					$id = $parent->getId();
					$parentid = $id;
					if(isset($treeData[$id]))
					{
						// In this case the parent of the component is already 
						// an item of the sales object. We are going to reset this 
						// item at the parent level
						break;
					}
				}
				$data['parentid']  = $parentid;
				$data['hierarchy'] = $hierarchyLabel[$data['hierarchy']];
				$treeData[$materialid] = $root->add(new Fox\Node($data));
			}
		
			$boms = $material->bom();
			foreach($boms->getChildren() as $bom)
			{
				$componentid = $bom->getProperty("componentid");
				
				$parentid = $bom->getProperty("materialid");
				$hieararchy = $bom->getProperty('hierarchy');
				$bom->setProperty('parentid',$materialid);
				$bom->setProperty('materialid',$componentid);
				$bom->setProperty('hierarchy',$hierarchyLabel[$hieararchy]);
				
				if(isset($selectedComponent[$componentid]))
				{
					if(isset($treeData[$componentid]))
					{
						
					}
					else
					{
						if(isset($treeData[$parentid]))
						{
							$treeData[$componentid] = $treeData[$parentid]->add($bom);
						}
						else
						{
							$treeData[$componentid] = $root->add($bom);
						}
					}
					
				}
			}
		}
		$root->sortChildren("hierarchy");
		$newRoot = new Fox\Node(Fox\Node::ROOT_NODE);
		// Need to rebuild the tree base on new added product
		foreach($root->getChildren() as $key => $child)
		{
			$parentid = $child->getProperty('parentid');
			$materialid = $child->getProperty('materialid');
			if($parentid != $materialid && isset($treeData[$parentid]))
			{
				$treeData[$materialid] = $treeData[$parentid]->add($child);
				$root->unsetChild($key);
			}
		}
	}
	$blItems->item("gItems")->setDatas($root);
	
	echo $blItems->Display();
	
}